<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB,Session,Input,Image,File;

class GoodsAlbumController extends Controller
{
    /**
     * 上传图片页面
     * @param type $id
     * @return type
     */
    public function create($id)
    {
        $goods_name = DB::table('goods')->where('id', $id)->pluck('goods_name');
        return view('admin.goods.albumadd', ['goods_name'=>$goods_name, 'goods_id'=>$id]);
    }
    
    public function uploadMore(Request $request,$id)
    {
        // 获取图片，并上传
        $file = $request->file('mallPic');
        $suffix = $file->getClientOriginalExtension();
        $savepath = './uploads/goodsimg/' . date('Ymd');
        if(!is_dir($savepath)){
            File::makeDirectory($savepath,  0777, true);
        }
        
        $rename = date('YmdHis') . mt_rand(1000,9999) . '.' . $suffix;
        $result = $file->move($savepath, $rename);
        
        if($result){
            $data['goods_id'] = $id;
            $data['album_image'] = $bigImg = $savepath . '/' . $rename;
            $data['album_thumb'] = $thumbImg = $savepath . '/' . 'thumb_' . $rename;
            // 图像处理
            // 商品大图 800*800
            Image::make($bigImg)->resize(800, 800)->save($bigImg);
            Image::make($bigImg)->resize(180, 180)->save($thumbImg);
            // 插入数据库
            $data['album_image'] = ltrim($bigImg,'.');
            $data['album_thumb'] = ltrim($thumbImg,'.');
            DB::table('goods_album')->insert($data);
            return response()->json(['savepath'=>$data['album_image'], 'thumbsavepath'=>$data['album_thumb']]);
        } else {
            return response()->json(['savepath'=>'','thumbsavepath'=>'']);
        }
    }
}
